package testes.testeUnidade;

import junit.framework.TestCase;
import logica.exceptions.InvalidDataException;
import logica.exceptions.RequiredDataException;
import logica.interfaces.UserIF;
import logica.users.User;

public class UserTest extends TestCase {

	protected UserIF userIFU1, userIFU2, userIFU3, userIFU4;

	public void setUp() throws RequiredDataException, InvalidDataException {
		userIFU1 = new User("m123", "Maria Maria", "mariamail@dsc.ufcg.edu.br",
				"123123", "Rua XYZ", "222222222-22", "33218888");
		userIFU2 = new User("j123", "Joao Joao", "joaomail@dsc.ufcg.edu.br",
				"654321", "Rua ABC", "333333333-33", "33337777");
		userIFU3 = new User("mylogin", "myname", "myemail@dsc.ufcg.edu.br",
				"123456", "Rua YWK", "444444444-44", "31245645");
	}

	public void testUser() {
		// teste de null.
		try {
			userIFU4 = new User(null, "myname", "myemail@dsc.ufcg.edu.br",
					"123456", "Rua YWK", "444444444-44", "31245645");
			fail("login nao pode ser null ou  vazia");
		} catch (RequiredDataException e) {
			assertEquals("8", "Required data: login", e.getMessage());
		} catch (InvalidDataException i) {
			i.printStackTrace();
		}
		try {
			userIFU4 = new User("mylogin", null, "myemail@dsc.ufcg.edu.br",
					"123456", "Rua YWK", "444444444-44", "31245645");
			fail("nome nao pode ser null ou  vazia");
		} catch (RequiredDataException e) {
			assertEquals("9", "Required data: name", e.getMessage());
		} catch (InvalidDataException i) {
			i.printStackTrace();
		}
		try {
			userIFU4 = new User("mylogin", "myname", null, "123456", "Rua YWK",
					"444444444-44", "31245645");
			fail("mail nao pode ser null ou  vazia");
		} catch (RequiredDataException e) {
			assertEquals("10", "Required data: email", e.getMessage());
		} catch (InvalidDataException i) {
			i.printStackTrace();
		}
		try {
			userIFU4 = new User("mylogin", "myname", "myemail@dsc.ufcg.edu.br",
					null, "Rua YWK", "444444444-44", "31245645");
			fail("password nao pode ser null ou  vazia");
		} catch (RequiredDataException e) {
			assertEquals("11", "Required data: password", e.getMessage());
		} catch (InvalidDataException i) {
			i.printStackTrace();
		}
		try {
			userIFU4 = new User("mylogin", "myname", "myemail@dsc.ufcg.edu.br",
					"123456", null, "444444444-44", "31245645");
			fail("address nao pode ser null ou  vazia");
		} catch (RequiredDataException e) {
			assertEquals("12", "Required data: address", e.getMessage());
		} catch (InvalidDataException i) {
			i.printStackTrace();
		}
		try {
			userIFU4 = new User("mylogin", "myname", "myemail@dsc.ufcg.edu.br",
					"123456", "Rua YWK", null, "31245645");
			fail("CPF number nao pode ser null ou  vazia");
		} catch (RequiredDataException e) {
			assertEquals("12", "Required data: CPF number", e.getMessage());
		} catch (InvalidDataException i) {
			i.printStackTrace();
		}

		// teste de vazio.
		try {
			userIFU4 = new User("", "myname", "myemail@dsc.ufcg.edu.br",
					"123456", "Rua YWK", "444444444-44", "31245645");
			fail("login nao pode ser null ou  vazia");
		} catch (RequiredDataException e) {
			assertEquals("8", "Required data: login", e.getMessage());
		} catch (InvalidDataException i) {
			i.printStackTrace();
		}
		try {
			userIFU4 = new User("mylogin", "", "myemail@dsc.ufcg.edu.br",
					"123456", "Rua YWK", "444444444-44", "31245645");
			fail("nome nao pode ser null ou  vazia");
		} catch (RequiredDataException e) {
			assertEquals("9", "Required data: name", e.getMessage());
		} catch (InvalidDataException i) {
			i.printStackTrace();
		}
		try {
			userIFU4 = new User("mylogin", "myname", "", "123456", "Rua YWK",
					"444444444-44", "31245645");
			fail("mail nao pode ser null ou  vazia");
		} catch (RequiredDataException e) {
			assertEquals("10", "Required data: email", e.getMessage());
		} catch (InvalidDataException i) {
			i.printStackTrace();
		}
		try {
			userIFU4 = new User("mylogin", "myname", "myemail@dsc.ufcg.edu.br",
					"", "Rua YWK", "444444444-44", "31245645");
			fail("password nao pode ser null ou  vazia");
		} catch (RequiredDataException e) {
			assertEquals("11", "Required data: password", e.getMessage());
		} catch (InvalidDataException i) {
			i.printStackTrace();
		}
		try {
			userIFU4 = new User("mylogin", "myname", "myemail@dsc.ufcg.edu.br",
					"123456", "", "444444444-44", "31245645");
			fail("address nao pode ser null ou  vazia");
		} catch (RequiredDataException e) {
			assertEquals("12", "Required data: address", e.getMessage());
		} catch (InvalidDataException i) {
			i.printStackTrace();
		}
		try {
			userIFU4 = new User("mylogin", "myname", "myemail@dsc.ufcg.edu.br",
					"123456", "Rua YWK", "", "31245645");
			fail("CPF number nao pode ser null ou  vazia");
		} catch (RequiredDataException e) {
			assertEquals("12", "Required data: CPF number", e.getMessage());
		} catch (InvalidDataException i) {
			i.printStackTrace();
		}
	}

	public void testAddStatus() {
		try {
			userIFU1.addStatus(2);
		} catch (RequiredDataException rde) {
			rde.printStackTrace();
		}
		assertEquals("1", userIFU1.getStatus(), 2);
	}

	public void testGetAddress() {
		assertEquals("1", userIFU1.getAddress(), "Rua XYZ");
		assertEquals("2", userIFU2.getAddress(), "Rua ABC");
	}

	public void testGetCpfNumber() {
		assertEquals("1", userIFU1.getCpfNumber(), "222222222-22");
		assertEquals("2", userIFU2.getCpfNumber(), "333333333-33");
		assertEquals("3", userIFU3.getCpfNumber(), "444444444-44");
	}

	public void testGetEmail() {
		assertEquals("1", userIFU1.getEmail(), "mariamail@dsc.ufcg.edu.br");
		assertEquals("2", userIFU2.getEmail(), "joaomail@dsc.ufcg.edu.br");
	}

	public void testGetLogin() {
		assertEquals("1", userIFU1.getLogin(), "m123");
		assertEquals("2", userIFU2.getLogin(), "j123");
	}

	public void testGetName() {
		assertEquals("1", userIFU1.getName(), "Maria Maria");
	}

	public void testGetPassword() {
		assertEquals("1", userIFU1.getPassword(), "123123");
		assertEquals("2", userIFU2.getPassword(), "654321");
	}

	public void testGetPhoneNumber() {
		assertEquals("1", userIFU1.getPhoneNumber(), "33218888");
		assertEquals("2", userIFU2.getPhoneNumber(), "33337777");
	}

	public void testGetStatus() {
		assertEquals("1", userIFU1.getStatus(), 0);
		assertEquals("2", userIFU2.getStatus(), 0);
	}

	public void testListProduct() {
		userIFU1.addProduct("produto 1");
		userIFU1.addProduct("produto 2");
		userIFU1.addProduct("produto 3");
		userIFU2.addProduct("produto 5");
		userIFU2.addProduct("produto 6");
		userIFU2.addProduct("produto 7");
		assertEquals("1", userIFU1.getAllProducts().toString(), "[produto 1, produto 2, produto 3]");
		assertEquals("2", userIFU1.listProduct(0), "produto 1");
		assertEquals("3", userIFU1.listProduct(1), "produto 2");
		assertEquals("4", userIFU1.listProduct(2), "produto 3");
		assertEquals("5", userIFU2.getAllProducts().toString(), "[produto 5, produto 6, produto 7]");
		assertEquals("6", userIFU2.listProduct(0), "produto 5");
		assertEquals("7", userIFU2.listProduct(1), "produto 6");
		assertEquals("8", userIFU2.listProduct(2), "produto 7");
	}

	public void testSetAddress() {
		try {
			userIFU1.setAddress("endereco3");
		} catch (RequiredDataException rde) {
			rde.printStackTrace();
		}
		assertEquals("3", userIFU1.getAddress(), "endereco3");
	}

	public void testSetCpfNumber() {
		try {
			userIFU1.setCpfNumber("111111111-11");
		} catch (RequiredDataException rde) {
			rde.printStackTrace();
		} catch (InvalidDataException ide) {
			ide.printStackTrace();
		}
		assertEquals("1", userIFU1.getCpfNumber(), "111111111-11");
	}

	public void testSetEmail() {
		try {
			userIFU1.setEmail("123@4");
		} catch (InvalidDataException ide) {
			ide.printStackTrace();
		} catch (RequiredDataException rde) {
			rde.printStackTrace();
		}
		assertEquals("1", userIFU1.getEmail(), "123@4");
	}

	public void testSetLogin() {
		try {
			userIFU1.setLogin("asdasd");

		} catch (RequiredDataException rde) {
			rde.printStackTrace();
		}
		assertEquals("1", userIFU1.getLogin(), "asdasd");
	}

	public void testSetName() throws RequiredDataException {
		userIFU3.setName("Gustavo");
		assertEquals("1", userIFU3.getName(), "Gustavo");
		try {
			userIFU3.setName(null);
			fail("Usuario nao pode ser null ou  vazia");
		} catch (Exception e) {
			assertEquals("2", "Required data: name", e.getMessage());
		}
		try {
			userIFU3.setName("");
			fail("Usuario nao pode ser null ou  vazia");
		} catch (Exception e) {
			assertEquals("3", "Required data: name", e.getMessage());
		}
	}

	public void testSetPassword() {
		try {
			userIFU1.setPassword("123@4");
		} catch (RequiredDataException rde) {
			rde.printStackTrace();
		}
		assertEquals("1", userIFU1.getPassword(), "123@4");
	}

	public void testSetPhoneNumber() {
		try {
			userIFU1.setPhoneNumber("22225555");
		} catch (RequiredDataException rde) {
			rde.printStackTrace();
		}
		assertEquals("1", userIFU1.getPhoneNumber(), "22225555");
	}

	public void testEqualsObject() throws RequiredDataException,
			InvalidDataException {

		UserIF userIFU5 = new User("m123", "Maria Maria",
				"mariamail@dsc.ufcg.edu.br", "123123", "Rua XYZ",
				"222222222-22", "33218888");

		assertFalse("1", userIFU2.equals(userIFU1));
		assertTrue("2", userIFU1.equals(userIFU5));
	}

}
